package com.fowo.api.util;


import com.fowo.api.service.LingXingInterfaceService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcUtils {

    private static String urlLocal = "jdbc:mysql://localhost:3306/sufa?characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true";
//    private static String url = "jdbc:mysql://118.195.190.225:50306/sf_supply_chain?characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true";
    private static String url = "jdbc:mysql://121.204.217.50:15033/sf_supply_chain?characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true";
    private static String user = "root";
    private static String passwordLocat = "123456";
   private static String password = "fowo2021";
//    @Resource
//    private LingXingInterfaceService lingXingInterfaceService;

    public static Connection getConnnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Connection getConnnectionLocal() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(urlLocal, user, passwordLocat);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void close(Connection conn, PreparedStatement ps, PreparedStatement pps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pps != null) {
            try {
                pps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
